package com.thx.dictionary.dao;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.PaginationDao;
import com.thx.dictionary.model.Dictionary;

@Repository
public class DictionaryDao extends PaginationDao<Dictionary> {
	
	public Dictionary findByDicName(String dicName) {
		String hql = "from Dictionary where dicName=?";
		Query query = createQuery(hql, dicName);
		List<Dictionary> dicns = query.list();
		if (dicns != null && dicns.size() > 0)
			return dicns.get(0);
		return null;
	}
	
	public List<Dictionary> searchDicName(String dicName) {
		String hql = "from Dictionary";
		if (dicName != null) {
			hql += " where dicName like '%" + StringUtils.strip(dicName) + "%'";
		}
		hql += " order by createDate desc";
		return findPageList(hql);
	}
	//--通过字典code模糊查到Dictionary的list
	public List<Dictionary> searchByCode(String dicnCode){
		List<Object> params = new ArrayList<Object>();
		String hql = "from Dictionary ";
		if(StringUtils.isNotBlank(dicnCode)){
			hql+=" where dicnCode like ?";
			params.add("%"+dicnCode+"%");
		}
		 Query query = this.createQuery(hql.toString(),params.toArray());
		 List<Dictionary>  list = query.list();
		 return list;
	}
}
